package com.fxhy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fxhy.bean.AgentUser;
import com.fxhy.contants.Constants;
import com.fxhy.service.AgentRechargeService;
import com.fxhy.service.UserService;
import com.fxhy.utils.JWTUtil;
import com.fxhy.vo.ResponseBodyEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@Api(value="财务接口")
@RequestMapping("recharge")
@Slf4j
public class RechargeApi {
	@Autowired
	private UserService userService;
	
	@Autowired
	private AgentRechargeService agentRechargeService;
	
	@ApiOperation("通过日期查找用户分成详情")
	@RequestMapping(value="findRechargeByDate",method= {RequestMethod.GET,RequestMethod.POST})
	public ResponseBodyEntity findRechargeByDate(String phoneNum, String dateStr, String token){
		ResponseBodyEntity responseBodyEntity = new ResponseBodyEntity();
		
		AgentUser user = userService.findUserByPhone(phoneNum);
		if(user != null && user.getUserId() != null){
			if(!JWTUtil.verify(token, user.getUserId()+"", user.getLastLoginTime())){
				responseBodyEntity.setData("/login");
				responseBodyEntity.setMsg(" 登录失效! 请重新登录!");
				responseBodyEntity.setState(Constants.FAILURE);
				return responseBodyEntity;
			}
			
			if(user.getIsIdentifi() != null && user.getIsIdentifi() > 0){
				responseBodyEntity.setData(agentRechargeService.findRechargeByDate(user.getUserId(),dateStr));
				responseBodyEntity.setState(Constants.SUCCEED);
			}else{
				responseBodyEntity.setData("/login");
				responseBodyEntity.setMsg("未发现你的认证信息! 为了避免信息泄露!请重新登录!");
				responseBodyEntity.setState(Constants.FAILURE);
				return responseBodyEntity;
			}
		}else{		
			responseBodyEntity.setData(userService.updateUser(user));
			responseBodyEntity.setMsg("登陆失效!请刷新界面! 若刷新无效!请重新登录!");
			responseBodyEntity.setState(Constants.FAILURE);
			return responseBodyEntity;
		}
		return  responseBodyEntity;
	}
}
